{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "neural-networks-tutorial.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyMigAWqeY7WvQBiWBJFFzXD",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/towardsai/tutorials/blob/master/neural_networks_tutorial_part_1/neural_networks_tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ScvnRKjsV0zS"
      },
      "source": [
        "# Neural Networks from Scratch with Python Code and Math in Detail\n",
        "\n",
        "* Tutorial: https://towardsai.net/p/machine-learning/building-neural-networks-from-scratch-with-python-code-and-math-in-detail-i-536fae5d7bbf \n",
        "\n",
        "* Github: https://github.com/towardsai/tutorials/tree/master/neural_networks_tutorial_part_1 \n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TfbSw80eVNCS",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 312
        },
        "outputId": "ebfaf520-2318-4a80-ddec-8240ca14bafc"
      },
      "source": [
        "# Import required libraries:\n",
        "import numpy as np# Define input features:\n",
        "input_features = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
        "print (input_features.shape)\n",
        "print (input_features)# Define target output:\n",
        "target_output = np.array([[0,1,1,1]])# Reshaping our target output into vector:\n",
        "target_output = target_output.reshape(4,1)\n",
        "print(target_output.shape)\n",
        "print (target_output)# Define weights:\n",
        "weights = np.array([[0.1],[0.2]])\n",
        "print(weights.shape)\n",
        "print (weights)# Bias weight:\n",
        "bias = 0.3# Learning Rate:\n",
        "lr = 0.05# Sigmoid function:\n",
        "def sigmoid(x):\n",
        " return 1/(1+np.exp(-x))# Derivative of sigmoid function:\n",
        "def sigmoid_der(x):\n",
        " return sigmoid(x)*(1-sigmoid(x))# Main logic for neural network:\n",
        " # Running our code 10000 times:for epoch in range(10000):\n",
        " inputs = input_features#Feedforward input:\n",
        " in_o = np.dot(inputs, weights) + bias #Feedforward output:\n",
        " out_o = sigmoid(in_o) #Backpropogation \n",
        " #Calculating error\n",
        " error = out_o - target_output\n",
        " \n",
        " #Going with the formula:\n",
        " x = error.sum()\n",
        " print(x)\n",
        " \n",
        " #Calculating derivative:\n",
        " derror_douto = error\n",
        " douto_dino = sigmoid_der(out_o)\n",
        " \n",
        " #Multiplying individual derivatives:\n",
        " deriv = derror_douto * douto_dino #Multiplying with the 3rd individual derivative:\n",
        " #Finding the transpose of input_features:\n",
        " inputs = input_features.T\n",
        " deriv_final = np.dot(inputs,deriv)\n",
        " \n",
        " #Updating the weights values:\n",
        " weights -= lr * deriv_final #Updating the bias weight value:\n",
        " for i in deriv:\n",
        "  bias -= lr * i #Check the final values for weight and biasprint (weights)\n",
        "  \n",
        "print (bias) #Taking inputs:\n",
        "single_point = np.array([1,0]) #1st step:\n",
        "result1 = np.dot(single_point, weights) + bias #2nd step:\n",
        "result2 = sigmoid(result1) #Print final result\n",
        "print(result2) #Taking inputs:\n",
        "single_point = np.array([1,1]) #1st step:\n",
        "result1 = np.dot(single_point, weights) + bias #2nd step:\n",
        "result2 = sigmoid(result1) #Print final result\n",
        "print(result2) #Taking inputs:\n",
        "single_point = np.array([0,0]) #1st step:\n",
        "result1 = np.dot(single_point, weights) + bias #2nd step:\n",
        "result2 = sigmoid(result1) #Print final result\n",
        "print(result2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(4, 2)\n",
            "[[0 0]\n",
            " [0 1]\n",
            " [1 0]\n",
            " [1 1]]\n",
            "(4, 1)\n",
            "[[0]\n",
            " [1]\n",
            " [1]\n",
            " [1]]\n",
            "(2, 1)\n",
            "[[0.1]\n",
            " [0.2]]\n",
            "0.3\n",
            "[0.59868766]\n",
            "[0.64565631]\n",
            "[0.57444252]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YrRCooGFcb0f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 295
        },
        "outputId": "275a1ab6-cd9b-475f-9775-f198e161a9e3"
      },
      "source": [
        "# Import required libraries:\n",
        "import numpy as np# Define input features:\n",
        "input_features = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
        "print (input_features.shape)\n",
        "print (input_features)# Define target output:\n",
        "target_output = np.array([[0,1,1,1]])# Reshaping our target output into vector:\n",
        "target_output = target_output.reshape(4,1)\n",
        "print(target_output.shape)\n",
        "print (target_output)# Define weights:\n",
        "weights = np.array([[0.1],[0.2]])\n",
        "print(weights.shape)\n",
        "print (weights)# Define learning rate:\n",
        "lr = 0.05# Sigmoid function:\n",
        "def sigmoid(x):\n",
        " return 1/(1+np.exp(-x))# Derivative of sigmoid function:\n",
        "def sigmoid_der(x):\n",
        " return sigmoid(x)*(1-sigmoid(x))# Main logic for neural network:\n",
        "# Running our code 10000 times:for epoch in range(10000):\n",
        " inputs = input_features#Feedforward input:\n",
        " pred_in = np.dot(inputs, weights)#Feedforward output:\n",
        " pred_out = sigmoid(pred_in)#Backpropogation \n",
        " #Calculating error\n",
        " error = pred_out - target_output\n",
        " x = error.sum()\n",
        " \n",
        " #Going with the formula:\n",
        " print(x)\n",
        " \n",
        " #Calculating derivative:\n",
        " dcost_dpred = error\n",
        " dpred_dz = sigmoid_der(pred_out)\n",
        " \n",
        " #Multiplying individual derivatives:\n",
        " z_delta = dcost_dpred * dpred_dz#Multiplying with the 3rd individual derivative:\n",
        " inputs = input_features.T\n",
        " weights -= lr * np.dot(inputs, z_delta)\n",
        " \n",
        " \n",
        "#Taking inputs:\n",
        "single_point = np.array([1,0])#1st step:\n",
        "result1 = np.dot(single_point, weights)#2nd step:\n",
        "result2 = sigmoid(result1)#Print final result\n",
        "print(result2)#Taking inputs:\n",
        "single_point = np.array([0,0])#1st step:\n",
        "result1 = np.dot(single_point, weights)#2nd step:\n",
        "result2 = sigmoid(result1)#Print final result\n",
        "print(result2)#Taking inputs:\n",
        "single_point = np.array([1,1])#1st step:\n",
        "result1 = np.dot(single_point, weights)#2nd step:\n",
        "result2 = sigmoid(result1)#Print final result\n",
        "print(result2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(4, 2)\n",
            "[[0 0]\n",
            " [0 1]\n",
            " [1 0]\n",
            " [1 1]]\n",
            "(4, 1)\n",
            "[[0]\n",
            " [1]\n",
            " [1]\n",
            " [1]]\n",
            "(2, 1)\n",
            "[[0.1]\n",
            " [0.2]]\n",
            "[0.52497919]\n",
            "[0.5]\n",
            "[0.57444252]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ES5UHf2ufWXc",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 607
        },
        "outputId": "508da63f-9aaa-4bb0-8c56-294ab7fc0ce6"
      },
      "source": [
        "# Import required libraries:\n",
        "import numpy as np# Define input features:\n",
        "input_features = np.array([[1,0,0,1],[1,0,0,0],[0,0,1,1],\n",
        " [0,1,0,0],[1,1,0,0],[0,0,1,1],\n",
        " [0,0,0,1],[0,0,1,0]])\n",
        "print (input_features.shape)\n",
        "print (input_features)# Define target output:\n",
        "target_output = np.array([[1,1,0,0,1,1,0,0]])# Reshaping our target output into vector:\n",
        "target_output = target_output.reshape(8,1)\n",
        "print(target_output.shape)\n",
        "print (target_output)# Define weights:\n",
        "weights = np.array([[0.1],[0.2],[0.3],[0.4]])\n",
        "print(weights.shape)\n",
        "print (weights)# Bias weight:\n",
        "bias = 0.3# Learning Rate:\n",
        "lr = 0.05# Sigmoid function:\n",
        "def sigmoid(x):\n",
        " return 1/(1+np.exp(-x))# Derivative of sigmoid function:\n",
        "def sigmoid_der(x):\n",
        " return sigmoid(x)*(1-sigmoid(x))# Main logic for neural network:\n",
        "# Running our code 10000 times:for epoch in range(10000):\n",
        " inputs = input_features#Feedforward input:\n",
        " pred_in = np.dot(inputs, weights) + bias#Feedforward output:\n",
        " pred_out = sigmoid(pred_in)#Backpropogation \n",
        " #Calculating error\n",
        " error = pred_out - target_output\n",
        " \n",
        " #Going with the formula:\n",
        " x = error.sum()\n",
        " print(x)\n",
        " \n",
        " #Calculating derivative:\n",
        " dcost_dpred = error\n",
        " dpred_dz = sigmoid_der(pred_out)\n",
        " \n",
        " #Multiplying individual derivatives:\n",
        " z_delta = dcost_dpred * dpred_dz#Multiplying with the 3rd individual derivative:\n",
        " inputs = input_features.T\n",
        " weights -= lr * np.dot(inputs, z_delta)#Updating the bias weight value:\n",
        " for i in z_delta:\n",
        "  bias -= lr * i#Printing final weights: \n",
        "\n",
        "print (weights)\n",
        "print (\"\\n\\n\")\n",
        "print (bias)#Taking inputs:\n",
        "single_point = np.array([1,0,0,1])#1st step:\n",
        "result1 = np.dot(single_point, weights) + bias#2nd step:\n",
        "result2 = sigmoid(result1)#Print final result\n",
        "print(result2)#Taking inputs:\n",
        "single_point = np.array([0,0,1,0])#1st step:\n",
        "result1 = np.dot(single_point, weights) + bias#2nd step:\n",
        "result2 = sigmoid(result1)#Print final result\n",
        "print(result2)#Taking inputs:\n",
        "single_point = np.array([1,0,1,0])#1st step:\n",
        "result1 = np.dot(single_point, weights) + bias#2nd step:\n",
        "result2 = sigmoid(result1)#Print final result\n",
        "print(result2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(8, 4)\n",
            "[[1 0 0 1]\n",
            " [1 0 0 0]\n",
            " [0 0 1 1]\n",
            " [0 1 0 0]\n",
            " [1 1 0 0]\n",
            " [0 0 1 1]\n",
            " [0 0 0 1]\n",
            " [0 0 1 0]]\n",
            "(8, 1)\n",
            "[[1]\n",
            " [1]\n",
            " [0]\n",
            " [0]\n",
            " [1]\n",
            " [1]\n",
            " [0]\n",
            " [0]]\n",
            "(4, 1)\n",
            "[[0.1]\n",
            " [0.2]\n",
            " [0.3]\n",
            " [0.4]]\n",
            "[[0.1]\n",
            " [0.2]\n",
            " [0.3]\n",
            " [0.4]]\n",
            "\n",
            "\n",
            "\n",
            "0.3\n",
            "[0.68997448]\n",
            "[0.64565631]\n",
            "[0.66818777]\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}